package com.ruoyi.webContent.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.webContent.domain.PlTeam;
import com.ruoyi.webContent.mapper.PlTeamMapper;
import com.ruoyi.webContent.service.IPlTeamService;
import com.ruoyi.webContent.utils.EasyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-11
 */
@Service
public class PlTeamServiceImpl implements IPlTeamService
{
    @Autowired
    private PlTeamMapper plTeamMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public PlTeam selectPlTeamById(Long id)
    {
        return plTeamMapper.selectPlTeamById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param type
     * @return 【请填写功能名称】集合
     */
    @Override
    public List<PlTeam> selectTeamByType(String type) {
        return plTeamMapper.selectTeamByType(type);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @return 【请填写功能名称】集合
     */
    @Override
    public List<PlTeam> selectLhList(String gameNameValue) {
        return plTeamMapper.selectLhList(gameNameValue);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param plTeam 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PlTeam> selectPlTeamList(PlTeam plTeam)
    {
        return plTeamMapper.selectPlTeamList(plTeam);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param plTeam 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertPlTeam(PlTeam plTeam)
    {
        return plTeamMapper.insertPlTeam(plTeam);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param plTeam 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updatePlTeam(PlTeam plTeam)
    {
        if(StringUtils.isNotBlank(plTeam.getTeamName())){
            plTeam.setTeamName(plTeam.getTeamName().trim());
            PlTeam p = new PlTeam();
            p.setTeamName(plTeam.getTeamName());
            p.setGameName(plTeam.getGameName());
            List<PlTeam> plTeams = plTeamMapper.selectPlTeamList(p);
            for (PlTeam tem_p:plTeams) {
                if(!tem_p.getId().equals(plTeam.getId())){
                    plTeam.setLdjId(tem_p.getLdjId());
                    plTeamMapper.deletePlTeamById(tem_p.getId());
                }
            }
        }
        if(StringUtils.isNotBlank(plTeam.getTeamLhName())){
            plTeam.setTeamLhName(plTeam.getTeamLhName().trim());
            PlTeam p = new PlTeam();
            p.setTeamLhName(plTeam.getTeamLhName());
            p.setGameName(plTeam.getGameName());
            List<PlTeam> plTeams = plTeamMapper.selectPlTeamList(p);
            for (PlTeam tem_p:plTeams) {
                if(!tem_p.getId().equals(plTeam.getId())){
                    plTeam.setLhId(tem_p.getLhId());
                    plTeamMapper.deletePlTeamById(tem_p.getId());
                }
            }
        }
        if(StringUtils.isNotBlank(plTeam.getTeamSortName())){
            plTeam.setTeamSortName(plTeam.getTeamSortName().trim());
            PlTeam p = new PlTeam();
            p.setTeamSortName(plTeam.getTeamSortName());
            p.setGameName(plTeam.getGameName());
            List<PlTeam> plTeams = plTeamMapper.selectPlTeamList(p);
            for (PlTeam tem_p:plTeams) {
                if(!tem_p.getId().equals(plTeam.getId())){
                    plTeam.setYbId(tem_p.getYbId());
                    plTeamMapper.deletePlTeamById(tem_p.getId());
                }
            }
        }
        return plTeamMapper.updatePlTeam(plTeam);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePlTeamByIds(String ids)
    {
        return plTeamMapper.deletePlTeamByIds(Convert.toStrArray(ids));
    }

    @Override
    public int merge(String ids) {
        PlTeam plTeamNew = new PlTeam();
        String[] strings = Convert.toStrArray(ids);
        for (String id:strings) {
            PlTeam plTeam = plTeamMapper.selectPlTeamById(Long.valueOf(id));
            BeanUtils.copyProperties(plTeam,plTeamNew, EasyUtils.getNullPropertyNames(plTeam));
        }
        plTeamNew.setId(null);
        int i=0;
        i=plTeamMapper.insertPlTeam(plTeamNew);
        i=plTeamMapper.deletePlTeamByIds(Convert.toStrArray(ids));
        return i;
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deletePlTeamById(Long id)
    {
        return plTeamMapper.deletePlTeamById(id);
    }

    @Override
    public int deletePlTeamBefore() {
        return plTeamMapper.deletePlTeamBefore();
    }

    @Override
    public void setTeam(String gameName,JSONArray ldj_allMatch,JSONArray yb_allMatch,JSONArray lh_allMatch){

        //雷电竞
        for (Object o:ldj_allMatch) {
            JSONObject json = JSONObject.parseObject(o.toString());
            JSONArray team = json.getJSONArray("team");
//            Map<String, String> l_teamMap = new HashMap<>();
            //雷电竞队伍信息存储（以yb为主）
            for (Object team_l : team) {
                JSONObject team_json = JSONObject.parseObject(team_l.toString());
                PlTeam plTeam = new PlTeam();
                plTeam.setGameName(gameName);
                String team_short_name = team_json.getString("team_short_name").trim();
                plTeam.setTeamName(team_short_name);
                List<PlTeam> plTeams = selectPlTeamList(plTeam);
                String ldjId = team_json.getString("team_id").trim();
                if(plTeams.size()<1){
                    plTeam.setLdjId(ldjId);
                    plTeam.setStartTime(json.getString("start_time"));
                    insertPlTeam(plTeam);
                }else{
                    for (PlTeam p:plTeams) {
                        if (!p.getLdjId().equals(ldjId)){
                            deletePlTeamById(p.getId());
                        }
                    }
                }
            }
        }
        //亚博
        for (Object o:yb_allMatch) {
            JSONObject json = JSONObject.parseObject(o.toString());
            //PHTName
            PlTeam plTeam = new PlTeam();
            plTeam.setGameName(gameName);
            String PHTName = json.getString("PHTName").trim();
            plTeam.setTeamSortName(PHTName);
            List<PlTeam> plTeams = selectPlTeamList(plTeam);
            if(plTeams.size()<1){
                PlTeam plTeam_new = new PlTeam();
                plTeam_new.setGameName(gameName);
                plTeam_new.setYbId(json.getString("PHTId").trim());
                plTeam_new.setTeamSortName(PHTName);
                String startTime=json.getString("PMCDate");
                startTime=startTime.replace("T"," ");
                Date date = DateUtils.addHours(DateUtils.parseDate(startTime.substring(0, 19)), 12);
                startTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date);
                plTeam_new.setStartTime(startTime);
                insertPlTeam(plTeam_new);
            }else{
                for (PlTeam p:plTeams) {
                    if (!p.getYbId().equals(json.getString("PHTId").trim())){
                        deletePlTeamById(p.getId());
                    }
                }
            }
            //PATName
            PlTeam plTeam2 = new PlTeam();
            plTeam2.setGameName(gameName);
            String PATName = json.getString("PATName").trim();
            plTeam2.setTeamSortName(PATName);
            List<PlTeam> plTeams2 = selectPlTeamList(plTeam2);
            if(plTeams2.size()<1){
                PlTeam plTeam_new = new PlTeam();
                plTeam_new.setGameName(gameName);
                plTeam_new.setYbId(json.getString("PATId").trim());
                plTeam_new.setTeamSortName(PATName);
                String startTime=json.getString("PMCDate");
                startTime=startTime.replace("T"," ");
                Date date = DateUtils.addHours(DateUtils.parseDate(startTime.substring(0, 19)), 12);
                startTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date);
                plTeam_new.setStartTime(startTime);
                insertPlTeam(plTeam_new);
            }else{
                for (PlTeam p:plTeams2) {
                    if (!p.getYbId().equals(json.getString("PATId").trim())){
                        deletePlTeamById(p.getId());
                    }
                }
            }

        }
        //雷火
        for (Object o:lh_allMatch) {
            JSONObject json = JSONObject.parseObject(o.toString());
            JSONObject home = json.getJSONObject("home");
            JSONObject away = json.getJSONObject("away");
            PlTeam plTeam = new PlTeam();
            plTeam.setGameName(gameName);
            String homeName = home.getString("team_name").trim();

            plTeam.setTeamLhName(homeName);
            List<PlTeam> homes = selectPlTeamList(plTeam);
            if(homes.size()<1){
                plTeam.setStartTime(json.getString("start_datetime").trim());
                plTeam.setLhId(home.getString("team_id").trim());
                insertPlTeam(plTeam);
            }
            //away
            PlTeam plTeam2 = new PlTeam();
            plTeam2.setGameName(gameName);
            String awayName = away.getString("team_name").trim();
            plTeam2.setTeamLhName(awayName);
            List<PlTeam> aways = selectPlTeamList(plTeam2);
            if(aways.size()<1){
                plTeam2.setStartTime(json.getString("start_datetime").trim());
                plTeam2.setLhId(away.getString("team_id").trim());
                insertPlTeam(plTeam2);
            }else{
                for (PlTeam p:aways) {
                    if (!p.getLhId().equals(away.getString("team_id").trim())){
                        deletePlTeamById(p.getId());
                    }
                }
            }
        }
        //数据库队伍去重
        //取出亚博不为空雷电竞为空的队伍
        List<PlTeam> yb_ldj = selectTeamByType("yb_ldj");
        //取出雷电竞不为空亚博为空的队伍
        List<PlTeam> ldj_yb = selectTeamByType("ldj_yb");
        //双层循环找出名字一样的
        for (PlTeam yb :yb_ldj) {
            for (PlTeam ldj :ldj_yb) {
                //名字一样的删除雷电竞数据，补全亚博数据
                if(yb.getTeamSortName().toLowerCase().replace(" ", "")
                        .equals(ldj.getTeamName().toLowerCase().replace(" ", ""))){
                    deletePlTeamById(ldj.getId());
                    yb.setTeamName(ldj.getTeamName());
                    yb.setLdjId(ldj.getLdjId());
                    yb.setTeamLhName(ldj.getTeamLhName());
                    yb.setLhId(ldj.getLhId());
                    updatePlTeam(yb);
                }
            }
        }
        //取出亚博不为空雷火为空的队伍
        List<PlTeam> yb_lh = selectTeamByType("yb_lh");
        //取出雷火不为空亚博为空的队伍
        List<PlTeam> lh_yb = selectTeamByType("lh_yb");
        //双层循环找出名字一样的
        for (PlTeam yb :yb_lh) {
            for (PlTeam lh :lh_yb) {
                //名字一样的删除雷火数据，补全亚博数据
                if(yb.getTeamSortName().toLowerCase().replace(" ", "")
                        .equals(lh.getTeamLhName().toLowerCase().replace(" ", ""))){
                    deletePlTeamById(lh.getId());
                    yb.setTeamLhName(lh.getTeamLhName());
                    yb.setLhId(lh.getLhId());
                    yb.setTeamName(lh.getTeamName());
                    yb.setLdjId(lh.getLdjId());
                    updatePlTeam(yb);
                }
            }
        }
        //数据库队伍去重
        //取出雷电竞不为空雷火为空的队伍
        List<PlTeam> ldj_lh = selectTeamByType("ldj_lh");
        //取出雷火不为空雷电竞为空的队伍
        List<PlTeam> lh_ldj = selectTeamByType("lh_ldj");
        //双层循环找出名字一样的
        for (PlTeam lh :lh_ldj) {
            for (PlTeam ldj :ldj_lh) {
                //名字一样的删除雷电竞数据，补全雷火数据
                if(lh.getTeamLhName().toLowerCase().replace(" ", "")
                        .equals(ldj.getTeamName().toLowerCase().replace(" ", ""))){
                    deletePlTeamById(ldj.getId());
                    lh.setTeamName(ldj.getTeamName());
                    lh.setLdjId(ldj.getLdjId());
                    lh.setTeamSortName(ldj.getTeamSortName());
                    lh.setYbId(ldj.getYbId());
                    updatePlTeam(lh);
                }
            }
        }

        //删除超过当前时间的队伍信息
        deletePlTeamBefore();
    }
}
